package com.security.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @Author: jxm
 * @Description:
 * @Date: 2022/4/28 11:15
 * @Version: 1.0
 */
//@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    // 设置用户 信息若是没有设置角色授权，就会报错误
    //异常：java.lang.IllegalArgumentException: Cannot pass a null GrantedAuthority collection
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encode = encoder.encode("123");
        //异常：java.lang.IllegalArgumentException: Cannot pass a null GrantedAuthority collection
        //auth.inMemoryAuthentication().withUser("lucy").password(encode);
        auth.inMemoryAuthentication().withUser("lucy").password(encode).roles("admin");
    }

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
}
